// 到期收款mock数据
let dueReceivableId = 9; // 用于生成新ID

const mockDueReceivables = [
  {
    id: 1,
    customerCode: 'KH001',
    serviceProject: '代理记账+报税',
    customerName: '北京科技有限公司',
    customerRating: 'A+',
    contactPerson: '张三',
    contactPhone: '13800138001',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 12000,
    memberEnterprise: '是',
    area: '北京',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 8000,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-15 10:30:00',
    receiptNumber: 'SP2024001',
    assistant: '赵六',
    paymentRemark: '客户已确认收到发票，承诺本周内通过银行转账付款',
    dueDate: '2024-03-31',
    overdueDays: 0,
  },
  {
    id: 2,
    customerCode: 'KH002',
    serviceProject: '财务咨询',
    customerName: '上海贸易有限公司',
    customerRating: 'A',
    contactPerson: '李明',
    contactPhone: '13900139002',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 8000,
    memberEnterprise: '否',
    area: '上海',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '未关注',
    receivedAmount: 8000,
    receivedUpTo: '2024-03',
    paymentStatus: '已收齐',
    lastPushTime: '2024-03-10 14:20:00',
    receiptNumber: 'SP2024002',
    assistant: '刘九',
    paymentRemark: '财务部门确认付款流程已启动，预计2个工作日内到账',
    dueDate: '2024-03-31',
    overdueDays: 0,
  },
  {
    id: 3,
    customerCode: 'KH003',
    serviceProject: '税务筹划',
    customerName: '深圳创新科技',
    customerRating: 'B+',
    contactPerson: '王华',
    contactPhone: '13700137003',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 15000,
    memberEnterprise: '是',
    area: '深圳',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '已关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-12 09:15:00',
    receiptNumber: '',
    assistant: '陈十二',
    paymentRemark: '客户要求提供电子发票，已发送至指定邮箱，等待付款',
    dueDate: '2024-04-30',
    overdueDays: 0,
  },
  {
    id: 4,
    customerCode: 'KH004',
    serviceProject: '审计服务',
    customerName: '广州制造有限公司',
    customerRating: 'A',
    contactPerson: '陈丽',
    contactPhone: '13600136004',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 20000,
    memberEnterprise: '是',
    area: '广州',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 15000,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-14 16:45:00',
    receiptNumber: 'SP2024003',
    assistant: '赵六',
    paymentRemark: '客户申请延期3天付款，已获得部门主管批准',
    dueDate: '2024-03-31',
    overdueDays: 0,
  },
  {
    id: 5,
    customerCode: 'KH005',
    serviceProject: '代理记账',
    customerName: '杭州电商科技',
    customerRating: 'B',
    contactPerson: '刘强',
    contactPhone: '13500135005',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 6000,
    memberEnterprise: '否',
    area: '杭州',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '未关注',
    receivedAmount: 6000,
    receivedUpTo: '2024-04',
    paymentStatus: '已收齐',
    lastPushTime: '2024-03-13 11:20:00',
    receiptNumber: 'SP2024004',
    assistant: '刘九',
    paymentRemark: '客户使用支付宝转账，已提供收款二维码，等待扫码付款',
    dueDate: '2024-04-30',
    overdueDays: 0,
  },
  {
    id: 6,
    customerCode: 'KH006',
    serviceProject: '财务培训',
    customerName: '北京教育科技',
    customerRating: 'C',
    contactPerson: '张敏',
    contactPhone: '13400134006',
    paymentMonth: '2024-05',
    servicePeriod: '2024年3月-2024年8月',
    receivableAmount: 10000,
    memberEnterprise: '否',
    area: '北京',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '未关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-11 08:30:00',
    receiptNumber: '',
    assistant: '陈十二',
    paymentRemark: '客户财务流程需要总经理签字，已在办理中，预计明天完成',
    dueDate: '2024-05-31',
    overdueDays: 0,
  },
  {
    id: 7,
    customerCode: 'KH007',
    serviceProject: '工商年检',
    customerName: '成都软件开发',
    customerRating: 'A-',
    contactPerson: '王磊',
    contactPhone: '13300133007',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 9000,
    memberEnterprise: '是',
    area: '成都',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 4500,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-16 15:20:00',
    receiptNumber: 'SP2024005',
    assistant: '赵六',
    paymentRemark: '客户要求现金付款，已安排财务人员上门收取',
    dueDate: '2024-04-30',
    overdueDays: 0,
  },
  {
    id: 8,
    customerCode: 'KH008',
    serviceProject: '税务申报',
    customerName: '武汉物流公司',
    customerRating: 'B+',
    contactPerson: '陈刚',
    contactPhone: '13200132008',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 7500,
    memberEnterprise: '否',
    area: '武汉',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '已关注',
    receivedAmount: 7500,
    receivedUpTo: '2024-03',
    paymentStatus: '已收齐',
    lastPushTime: '2024-03-17 09:45:00',
    receiptNumber: 'SP2024006',
    assistant: '刘九',
    paymentRemark: '客户通过网银转账，已提供详细收款信息，等待操作',
    dueDate: '2024-03-31',
    overdueDays: 0,
  },
];

export default {
  // 获取到期收款列表
  'GET /api/due-receivables': (req, res) => {
    const { status, current = 1, pageSize = 10, ...params } = req.query;
    
    // 根据状态过滤
    let filteredData = mockDueReceivables;
    if (status === '未收款') {
      filteredData = mockDueReceivables.filter(item => item.paymentStatus === '未收款');
    } else if (status === '部分收款') {
      filteredData = mockDueReceivables.filter(item => item.paymentStatus === '部分收款');
    } else if (status === '已收齐') {
      filteredData = mockDueReceivables.filter(item => item.paymentStatus === '已收齐');
    }
    
    // 搜索过滤
    if (params.customerName) {
      filteredData = filteredData.filter(item => 
        item.customerName.includes(params.customerName)
      );
    }
    if (params.customerCode) {
      filteredData = filteredData.filter(item => 
        item.customerCode.includes(params.customerCode)
      );
    }
    if (params.contactPerson) {
      filteredData = filteredData.filter(item => 
        item.contactPerson.includes(params.contactPerson)
      );
    }
    
    // 分页
    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const pagedData = filteredData.slice(startIndex, endIndex);
    
    res.json({
      success: true,
      data: {
        list: pagedData,
        total: filteredData.length,
        current: parseInt(current),
        pageSize: parseInt(pageSize),
      },
    });
  },

  // 创建到期收款记录
  'POST /api/due-receivables': (req, res) => {
    const newRecord = {
      id: dueReceivableId++,
      ...req.body,
      paymentStatus: '未收款',
      receivedAmount: 0,
      receivedUpTo: '',
      receiptNumber: '',
    };
    mockDueReceivables.unshift(newRecord);
    
    res.json({
      success: true,
      data: newRecord,
      message: '到期收款记录创建成功',
    });
  },

  // 更新到期收款记录
  'PUT /api/due-receivables/:id': (req, res) => {
    const { id } = req.params;
    const index = mockDueReceivables.findIndex(item => item.id == id);
    
    if (index !== -1) {
      mockDueReceivables[index] = { ...mockDueReceivables[index], ...req.body };
      res.json({
        success: true,
        data: mockDueReceivables[index],
        message: '到期收款记录更新成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '到期收款记录不存在',
      });
    }
  },

  // 删除到期收款记录
  'DELETE /api/due-receivables/:id': (req, res) => {
    const { id } = req.params;
    const index = mockDueReceivables.findIndex(item => item.id == id);
    
    if (index !== -1) {
      mockDueReceivables.splice(index, 1);
      res.json({
        success: true,
        message: '到期收款记录删除成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '到期收款记录不存在',
      });
    }
  },
}; 